package com.jingzhi.huimiao.fragment;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jingzhi.huimiao.ChooseRoleActivity;
import com.jingzhi.huimiao.R;
import com.jingzhi.huimiao.TaskActivity;
import com.jingzhi.huimiao.utils.DataStoreUtil;
import com.jingzhi.huimiao.utils.SQLdm;

/**
 * 主页面的主页标签页
 * 
 * @author cxk
 * 
 */
public class MainFragment extends Fragment implements OnGroupClickListener {

	private Button btn_recruit_page;
	private ImageView role;
	private TextView finishtime;
	private View view;
	private int[] ids;

	private static final int REFRESH_COMPLETE = 0X110;
	private ListView mListView;
	private MainAdapter mAdapter;
	private Intent intent;
	private Context context;
	private SQLiteDatabase db;
	private Cursor useridcur;
	private Cursor roleidcur;
	private long userid;
	private Long roleid;
	private TextView level;
	private TextView experience;
	private TextView studytime;
	private long lastid;
	private Cursor lastidcur;
	private long taskid;
	private long chapterid;
	private Cursor rightcur;
	private int rightcount;
	private int currentRC;
	private ImageView tv_title;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


		SQLdm s = new SQLdm();
		db = s.openDatabase(getActivity().getApplicationContext());

		view = View.inflate(getActivity(), R.layout.fragment_tab1, null);

		context = getActivity().getApplicationContext();
		intent = new Intent(context, TaskActivity.class);

		ids = new int[] { R.drawable.map_bg_1, R.drawable.map_bg_2,
				R.drawable.map_bg_3, R.drawable.map_bg_4, R.drawable.map_bg_5,
				R.drawable.map_bg_6, R.drawable.map_bg_7 };

		role = (ImageView) view.findViewById(R.id.iv_role);
		level = (TextView) view.findViewById(R.id.tv_level);
		experience = (TextView) view.findViewById(R.id.tv_experience);
		studytime = (TextView) view.findViewById(R.id.tv_time);
		// 设置数据
		studytime.setText("   "
				+ DataStoreUtil.getLong(getActivity(), "studytime"));

		

		userid = DataStoreUtil.getLong(getActivity(), "userid");
		roleid = DataStoreUtil.getLong(getActivity(), "roleid");
		Log.e("LASTID---r", roleid + "");
		if (roleid == 1) {
			role.setBackgroundResource(R.drawable.role_1_03);

			lastidcur = db.rawQuery("select * from role1 where user_id="
					+ userid + ";", null);
			if (lastidcur.moveToFirst()) {

				lastid = lastidcur.getLong(lastidcur.getColumnIndex("word_id"));
			}

		} else if (roleid == 2) {
			role.setBackgroundResource(R.drawable.role_2_03);
			Log.e("LASTID---v", userid + "");
			String idd = userid + "";
			lastidcur = db.rawQuery("select * from role2 where user_id=" + idd
					+ ";", null);

			Log.e("LASTID---v---", lastidcur.getColumnIndex("word_id") + "");
			if (lastidcur.moveToFirst()) {
				Log.e("LASTID", "frwse");
				// lastid = lastidcur.getLong(2);
				lastid = lastidcur.getLong(lastidcur.getColumnIndex("word_id"));
				Log.e("LASTID", lastid + "");
			}
		} else {
			role.setBackgroundResource(R.drawable.role_3_03);

			lastidcur = db.rawQuery("select * from role3 where user_id="
					+ userid + ";", null);
			if (lastidcur.moveToFirst()) {

				lastid = lastidcur.getLong(lastidcur.getColumnIndex("word_id"));
			}
		}
		Cursor taskidcur = db.rawQuery("select * from word where id=" + lastid
				+ ";", null);
		if (taskidcur.moveToFirst()) {

			taskid = taskidcur.getLong(taskidcur.getColumnIndex("pid"));
		}

		Cursor chapteridcur = db.rawQuery("select * from unit where id="
				+ taskid + ";", null);
		if (chapteridcur.moveToFirst()) {

			chapterid = chapteridcur
					.getLong(chapteridcur.getColumnIndex("pid"));
		}

		//查询表中该用户的正确答对的单词数，也就是经验值的五分之一
			rightcur = db.rawQuery("select * from word where word_right=2;", null);
			if(rightcur.moveToFirst()){
				rightcount = rightcur.getCount();
			}
		//查询sp中存储的经验值
			currentRC = DataStoreUtil.getInt(getActivity(), "experience");
		if(currentRC <= rightcount*5){
			currentRC = rightcount*5;
		}
		// 设置经验值和level
		level.setText("   " + (chapterid));
		experience.setText("   " + currentRC);
		if(rightcur != null){
			rightcur.close();
		}
		if (chapteridcur != null) {
			chapteridcur.close();
		}
		if (taskidcur != null) {
			taskidcur.close();
		}
		if (lastidcur != null) {
			lastidcur.close();
		}

		mListView = (ListView) view.findViewById(R.id.id_listview);

		mListView.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});

		mListView.setAdapter(new MainAdapter());

		return view;
	}

	class MainAdapter extends BaseAdapter implements OnClickListener {

		private Long value;

		@Override
		public int getCount() {
			return ids.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = View.inflate(getActivity(),
						R.layout.main_lv_item, null);
				holder = new ViewHolder();
				// holder.image = (ImageView)
				// convertView.findViewById(R.id.iv_main_bg);
				holder.chapter_one = (Button) convertView
						.findViewById(R.id.chapter_one);
				holder.chapter_one.setTag(position);
				holder.chapter_two = (Button) convertView
						.findViewById(R.id.chapter_two);
				holder.chapter_three = (Button) convertView
						.findViewById(R.id.chapter_three);
				holder.chapter_four = (Button) convertView
						.findViewById(R.id.chapter_four);
				holder.chapter_five = (Button) convertView
						.findViewById(R.id.chapter_five);
				holder.chapter_six = (Button) convertView
						.findViewById(R.id.chapter_six);
				holder.chapter_seven = (Button) convertView
						.findViewById(R.id.chapter_seven);

				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();

			}
			holder.chapter_one.setTag(position);
			holder.chapter_two.setTag(position);
			holder.chapter_three.setTag(position);
			holder.chapter_four.setTag(position);
			holder.chapter_five.setTag(position);
			holder.chapter_six.setTag(position);
			holder.chapter_seven.setTag(position);

			addListener(holder);

			convertView.setBackgroundResource(ids[position]);
			if (position > 0) {
				// holder.image.setVisibility(0);
				// holder.chapter_one.setClickable(false);
				holder.chapter_two.setClickable(false);
				holder.chapter_three.setClickable(false);
				holder.chapter_four.setClickable(false);
				holder.chapter_five.setClickable(false);
				holder.chapter_six.setClickable(false);
				holder.chapter_seven.setClickable(false);
			} else {
				// holder.image.setVisibility(8);
			}

			return convertView;
		}

		private void addListener(ViewHolder holder) {
			holder.chapter_one.setOnClickListener(this);
			holder.chapter_two.setOnClickListener(this);
			holder.chapter_three.setOnClickListener(this);
			holder.chapter_four.setOnClickListener(this);
			holder.chapter_five.setOnClickListener(this);
			holder.chapter_six.setOnClickListener(this);
			holder.chapter_seven.setOnClickListener(this);
		}

		class ViewHolder {
			private Button chapter_one;
			private Button chapter_two;
			private Button chapter_three;
			private Button chapter_four;
			private Button chapter_five;
			private Button chapter_six;
			private Button chapter_seven;
			private ImageView image;

		}

		// ViewHolder vd = new ViewHolder();
		@Override
		public void onClick(View v) {

			int count = (Integer) v.getTag();

			switch (v.getId()) {
			case R.id.chapter_one:
				value = (long) ((count + 1) * 7 + 1);
				break;
			case R.id.chapter_two:
				value = (long) ((count + 1) * 7 + 2);
				break;
			case R.id.chapter_three:
				value = (long) ((count + 1) * 7 + 3);
				break;
			case R.id.chapter_four:
				value = (long) ((count + 1) * 7 + 4);
				break;
			case R.id.chapter_five:
				value = (long) ((count + 1) * 7 + 5);
				break;
			case R.id.chapter_six:
				value = (long) ((count + 1) * 7 + 6);
				break;
			case R.id.chapter_seven:
				value = (long) ((count + 1) * 7 + 7);
				break;
			default:
				break;
			}

			DataStoreUtil.putint(getActivity(), "CHOOSRCHAPTER", value);

			startActivity(intent);
			getActivity().finish();
		}
	}

	@Override
	public boolean onGroupClick(ExpandableListView parent, View v,
			int groupPosition, long id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
		
		if (useridcur != null) {
			useridcur.close();
		}
		if (roleidcur != null) {
			roleidcur.close();
		}
		if (db != null) {
			db.close();
		}
	}
}
